unit Conns;

interface

uses
  SysUtils, Classes, DB, IBCustomDataSet, IBTable, DBClient, SQLHelpers, Dialogs,
  ExtCtrls, Buttons, ToolWin, ComCtrls, Controls, Grids, DBGrids, Forms, ImgList;

type
  TConnsForm = class(TForm)
    csConns: TClientDataSet;
    tbConns: TIBTable;
    dsConns: TDataSource;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    DBGrid2: TDBGrid;
    TabSheet2: TTabSheet;
    Splitter1: TSplitter;
    DBGrid1: TDBGrid;
    DBGrid3: TDBGrid;
    dsRepItems: TDataSource;
    tbRepItems: TIBTable;
    dsRepCmds: TDataSource;
    tbRepCmds: TIBTable;
    ImageList1: TImageList;
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
    procedure DBGrid2DblClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    Conns: TStrings;
  public
    procedure AddConnToDBHelper;
    function GetDBIndex(ConnCode: string): Integer;
  end;

var
  ConnsForm: TConnsForm;

implementation
uses
  Main, Conn;
{$R *.dfm}

procedure TConnsForm.AddConnToDBHelper;
begin
  tbConns.First;
  while not tbConns.Eof do
  begin
    DBHelper.AddDBConnection(
      Trim(tbConns.FieldByName('ConnType').AsString),
      Trim(tbConns.FieldByName('ConnectionStrings').AsString));
    Conns.Values[Trim(tbConns.FieldByName('ConnCode').AsString)] := IntToStr(DBHelper.DBConnCount - 1);
    tbConns.Next;
  end;
end;

procedure TConnsForm.DataModuleCreate(Sender: TObject);
begin
  Conns := TStringList.Create;
  tbConns.Open;
  tbRepItems.Open;
  tbRepCmds.Open;
end;

procedure TConnsForm.DataModuleDestroy(Sender: TObject);
begin
  Conns.Free;
end;

procedure TConnsForm.DBGrid2DblClick(Sender: TObject);
begin
  TConnForm.Create(Self).ShowModal;
end;

procedure TConnsForm.FormShow(Sender: TObject);
begin
  tbRepItems.Filtered := False;
  tbRepCmds.Filtered := False;
end;

function TConnsForm.GetDBIndex(ConnCode: string): Integer;
begin
  Result := StrToInt(Conns.Values[ConnCode]);
end;

end.
